package com.d16_Socket_Homework.homework4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class ServerDemo4_TCP {
    public static void main(String[] args) throws IOException {
        // 创建服务器端对象
        ServerSocket serverSocket = new ServerSocket(11111);
        Socket socket = serverSocket.accept();
        // 读取
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        // 保存文件
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(
                        "homework/server/" + UUID.randomUUID().toString() + ".jpg"));
        // 读写
        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        // socket输入流禁用
        socket.shutdownInput();
        // 释放资源
        bos.close();

        // 给客户端反馈
        PrintWriter pw = new PrintWriter(
                socket.getOutputStream(), true);
        pw.println("上传成功");
        // 释放资源
        socket.close();

    }
}
